<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Local_socket_api extends BaseController {

	public function __construct() {
        parent::__construct();
        // 加载数据库类
        $this->load->model('classesModel');
    }

    // 客户端连接回调
    public function connected(){
        $channelId = $this->post('channel_id');
        // var_export($channelId);
        if(empty($channelId)) {
            return false;
        }

        // 获取班牌信息
        $brandInfo = $this->classesModel->get_brand_info($channelId);
        if(!$brandInfo) return false;
        // 更新班牌在线状态
        $data['is_online'] = 1;
        $data['online_time'] = date("Y-m-d H:i:s");
        $this->db->update(ClassesModel::$brandTable, $data, "id='{$brandInfo['id']}'");
        return 'connected success';
    }

    // 客户端连接断开回调
    public function disconnected(){  
        $channelId = $this->post('channel_id');
        if(empty($channelId)) {
            return false;
        }

        // 获取班牌信息
        $brandInfo = $this->classesModel->get_brand_info($channelId);
        if(!$brandInfo) return false;
        // 更新班牌在线状态
        $data['is_online'] = 0;
        $data['offline_time'] = date("Y-m-d H:i:s");
        $this->db->update(ClassesModel::$brandTable, $data, "id='{$brandInfo['id']}'");
        return 'disconnected success';
        // // 写入日志记录
        // file_get_contents("http://bw0663.com/test.php?school={$brandInfo['id']}&value=断开". json_encode($_POST));
    }
}